<template>
  <div class='edit-channel'>
    <!-- 我的频道 -->
    <van-cell class='my-cell'>
      <span class='left-title' slot='title'>我的频道</span>
      <van-button @click='isEdit=!isEdit' type="default" size='mini' plain style='color:red'>{{isEdit ? '完成':'编辑'}}
      </van-button>
    </van-cell>
    <van-grid class='list-grid my-grid' :gutter="10">
      <van-grid-item :class="{active:index===active}" v-for='(item,index) in channels' :key='item.id'
        @click='myChannelClick(index,item.id)' :icon='(isEdit && index !== 0)?"close":""' :text="item.name" />
    </van-grid>
    <!-- 推荐频道 -->
    <van-cell class='my-cell'>
      <span class='left-title' slot='title'>推荐频道</span>
    </van-cell>
    <van-grid class='list-grid recommend-grid' :gutter="10" direction='horizontal'>
      <van-grid-item icon='plus' @click='recommendClick(item)' :text="item.name" v-for='item in recommendChannels'
        :key='item.id' />

    </van-grid>
  </div>
</template>

<script>
// 所有频道列表的请求
import { getAllChannels, addUserChannel, delUserChannel } from '@/api/channel.js'
// 是否登录
import { mapState } from 'vuex'
// 本地存储
import { setItem } from '@/utils/storage.js'
const HMTT_CHANNEL = 'hmtt_channel'
export default {
  data () {
    return {
      // 所有频道
      allChannels: [],
      // 编辑显示
      isEdit: false
    }
  },
  props: {
    // 父传子我的频道
    channels: {
      type: Array,
      required: true
    },
    // 父传子选择的频道
    active: {
      type: Number,
      required: true
    }
  },
  methods: {
    // 点击我的频道
    myChannelClick (index, id) {
      if (this.isEdit) {
        if (index < this.active) {
          this.$emit('sendData', {
            close: true,
            index: this.active - 1
          })
        }
        this.channels.splice(index, 1)
        this.deleteChannelStorage(id)
      } else {
        this.$emit('sendData', {
          close: false,
          index
        })
      }
    },
    // 删除数据
    deleteChannelStorage (id) {
      if (this.user) {
        try {
          delUserChannel(id)
          this.$toast.success('删除成功')
        } catch (err) {
          this.$toast('删除失败')
        }
      } else {
        setItem(HMTT_CHANNEL, this.channels)
      }
    },
    // 添加我的频道
    async recommendClick (item) {
      this.channels.push(item)
      if (this.user) {
        try {
          await addUserChannel({
            id: item.id,
            seq: this.channels.length
          })
          this.$toast.success('添加成功')
        } catch (err) {
          this.$toast('添加失败')
        }
      } else {
        setItem(HMTT_CHANNEL, this.channels)
      }
    },

    // 获取所有频道列表
    async loadChannels () {
      try {
        const { data: res } = await getAllChannels()
        this.allChannels = res.data.channels
        console.log(res)
      } catch (err) {
        this.$toast('获取失败')
      }
    }
  },
  computed: {
    // 计算出推荐频道
    recommendChannels () {
      return this.allChannels.filter(item => {
        return !this.channels.find(channelitem => item.id === channelitem.id)
      })
    },
    ...mapState(['user'])
  },
  watch: {},
  filters: {},
  components: {},
  created () {
    // 获取所有频道列表
    this.loadChannels()
  },
  mounted () { }
}
</script>

<style scoped lang='less'>
.edit-channel {
  margin-top: 60px;
  .my-cell {
    align-items: center;
    &::after {
      border: none;
    }
    .left-title {
      color: #333;
      font-size: 33px;
    }
    .van-button--default {
      padding: 5px 20px;
    }
  }
  /deep/.recommend-grid {
    .van-grid-item__icon {
      font-size: 24px;
    }
  }
  /deep/.my-grid {
    .van-grid-item__icon + .van-grid-item__text {
      margin: 0;
    }
    .van-icon-close {
      font-size: 34px;
      position: absolute;
      top: -17px;
      right: -17px;
    }
    .active {
      .van-grid-item__text {
        color: red;
      }
    }
  }
  /deep/.list-grid {
    .van-grid-item {
      height: 86px;
      .van-grid-item__content {
        background-color: #f4f5f6;
        border-radius: 6px;
        &::after {
          border: none;
        }
      }
    }
  }
}
</style>
